##################################################
# Makefile
##################################################

SRC:=./src
OUT:=./output
BOOT:=boot.asm
LDR:=loader.asm
KER:=kernal.asm
BOOT_BIN:=$(subst .asm,.bin,$(BOOT))
LDR_BIN:=$(subst .asm,.bin,$(LDR))
KER_OBJ:=$(subst .asm,.o,$(KER))
KER_BIN:=$(subst .asm,.bin,$(KER))
IMG:=a.img
FLOPPY:=/mnt/floppy/
.PHONY : everything run

$(OUT)/global.o : $(SRC)/kernal/global.c $(SRC)/include/global.h 
	gcc -I$(SRC) -c -m32 -fno-builtin  -o $@ $(SRC)/kernal/global.c
$(OUT)/init8259A.o : $(SRC)/kernal/init_8259A.c $(SRC)/include/init_8259A.h 
	gcc -I$(SRC) -c -m32 -fno-builtin  -o $@ $(SRC)/kernal/init_8259A.c
$(OUT)/gdtcpy.o : $(SRC)/kernal/gdtcpy.c
	gcc -I$(SRC) -c -m32 -fno-builtin  -o $@ $(SRC)/kernal/gdtcpy.c
$(OUT)/initidt.o : $(SRC)/kernal/init_idt.c
	gcc -I$(SRC) -c -m32 -fno-builtin  -o $@ $(SRC)/kernal/init_idt.c
$(OUT)/klibc.o : $(SRC)/klib/klib.c
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/klib/klib.c
$(OUT)/kernalmain.o : $(SRC)/kernal/kernal_main.c 
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $<
$(OUT)/clock.o : $(SRC)/kernal/clock.c 
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $<
$(OUT)/irq.o : $(SRC)/kernal/irq.c 
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $<
$(OUT)/klib.o : $(SRC)/klib/klib.asm $(SRC)/include/klib.h
	nasm -f elf $(SRC)/klib/klib.asm -o $(OUT)/klib.o
$(OUT)/ireb.o : $(SRC)/kernal/irq.asm
	nasm -f elf $(SRC)/kernal/irq.asm -o $(OUT)/ireb.o -I $(SRC)
$(OUT)/proc.o : $(SRC)/kernal/proc.c $(SRC)/include/proc.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/proc.c
$(OUT)/keyboard.o : $(SRC)/kernal/keyboard.c $(SRC)/include/keyboard.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/keyboard.c
$(OUT)/tty.o : $(SRC)/kernal/tty.c $(SRC)/include/tty.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/tty.c 
$(OUT)/printf.o : $(SRC)/kernal/printf.c $(SRC)/include/printf.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/printf.c
$(OUT)/vsprintf.o : $(SRC)/kernal/vsprintf.c $(SRC)/include/vsprintf.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/vsprintf.c
$(OUT)/console.o : $(SRC)/kernal/console.c $(SRC)/include/console.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/console.c
$(OUT)/syscall.o : $(SRC)/kernal/syscall.c $(SRC)/include/syscall.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/syscall.c
$(OUT)/syscallb.o : $(SRC)/kernal/syscall.asm
	nasm -f elf $(SRC)/kernal/syscall.asm -o $(OUT)/syscallb.o -I $(SRC)
$(OUT)/misc.o : $(SRC)/lib/misc.c $(SRC)/include/misc.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/lib/misc.c
$(OUT)/systask.o : $(SRC)/kernal/systask.c $(SRC)/include/systask.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/systask.c
$(OUT)/hd.o : $(SRC)/kernal/hd.c $(SRC)/include/hd.h
	gcc -I$(SRC) -c -m32 -fno-builtin -fno-stack-protector  -o $@ $(SRC)/kernal/hd.c
$(OUT)/$(KER_OBJ) : $(SRC)/kernal/$(KER)
	nasm -f elf $(SRC)/kernal/$(KER) -o $(OUT)/$(KER_OBJ) -I $(SRC)


KERNAL_OBJS :=  $(OUT)/$(KER_OBJ) $(OUT)/global.o  $(OUT)/gdtcpy.o $(OUT)/klib.o  $(OUT)/klibc.o $(OUT)/kernalmain.o $(OUT)/initidt.o $(OUT)/init8259A.o  \
			    $(OUT)/clock.o \
				$(OUT)/proc.o \
			    $(OUT)/irq.o \
				$(OUT)/ireb.o \
				$(OUT)/syscall.o \
				$(OUT)/syscallb.o \
				$(OUT)/keyboard.o \
				$(OUT)/tty.o	\
				$(OUT)/console.o \
				$(OUT)/printf.o \
				$(OUT)/vsprintf.o \
				$(OUT)/misc.o \
				$(OUT)/systask.o \
				$(OUT)/hd.o


$(OUT)/$(KER_BIN): $(KERNAL_OBJS)
	ld -m elf_i386 -Ttext 0x30400 -s $(KERNAL_OBJS) -o $(OUT)/$(KER_BIN)

$(OUT)/$(BOOT_BIN) : $(SRC)/boot/$(BOOT)
	nasm $< -o $@ -I $(SRC)/boot

$(OUT)/$(LDR_BIN) : $(SRC)/boot/$(LDR)
	nasm $< -o $@ -I $(SRC)/boot

everything : $(OUT)/$(BOOT_BIN) $(OUT)/$(LDR_BIN) $(OUT)/$(KER_BIN)	
	dd if=$(OUT)/$(BOOT_BIN) of=$(OUT)/$(IMG) bs=512 count=1 conv=notrunc
	sudo mount -o loop $(OUT)/$(IMG) $(FLOPPY)
	sudo cp $(OUT)/$(LDR_BIN) $(FLOPPY) -v
	sudo cp $(OUT)/$(KER_BIN) $(FLOPPY) -v
	sudo umount $(FLOPPY)

run : 
	-bochs -f bochsrc

clean :
	rm -f $(OUT)/$(BOOT_BIN) $(OUT)/$(LDR_BIN) $(OUT)/$(KER_BIN) $(KERNAL_OBJS) 

